﻿using System;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using Ensemble;

namespace CommonUtils
{
    public class Print
    {
        [DllImport("kernel32.dll")]
        public static extern bool SetConsoleTextAttribute(
            IntPtr hConsoleOutput,
            int wAttributes
            );

    
        [DllImport("kernel32.dll")]
        public static extern IntPtr GetStdHandle(uint nStdHandle);

        public static bool DEBUG = false;
        public static uint STD_OUTPUT_HANDLE = 0xfffffff5;
        public static IntPtr hConsole = GetStdHandle(STD_OUTPUT_HANDLE);

        public static void DebugEmit(string error)
        {
            if (DEBUG)
            {
                Console.WriteLine(error);
            }
        }

        public static void ColorEmit(string str, Foreground color)
        {
            SetConsoleTextAttribute(hConsole, (int)color);
            Console.WriteLine(str);
            SetConsoleTextAttribute(hConsole, (int)Foreground.Default);
        }

        public static string ArrayAsString(string[] words)
        {
            StringBuilder sb = new StringBuilder(String.Empty);
            foreach (string word in words)
            {
                sb.Append(word);
                if (!words.Last().Equals(word))
                {
                    sb.Append(" "); 
                } 
            }
            return sb.ToString();
        }

        public static void PrintNewViewMsg(Member memb, Message msg)
        {
            StringBuilder sb = new StringBuilder(String.Empty);
            sb.Append("Install:{ " + memb.current_view.name + "\n");
            sb.Append("  nmembers= " + msg.view.nmembers + "\n");
            sb.Append("  view= " + ArrayAsString(msg.view.view));
            Print.ColorEmit(
                sb.ToString(),
                Print.Foreground.Green
            );
        }

        public enum Foreground
        {
            Blue = 0x0000000B,
            Green = 0x0000000A,
            Red = 0x0000000C,
            Default = 0x00000007
        }

        
    }
}
